package com.example.Lezedata.ui.orderPage;

import androidx.fragment.app.FragmentActivity;
import androidx.lifecycle.ViewModelProvider;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;

import com.example.Lezedata.R;
import com.example.Lezedata.dataManager.GBUserManager;
import com.example.Lezedata.net.GBError;
import com.example.Lezedata.net.GBNetRequest;
import com.example.Lezedata.net.ServerResponseObserver;
import com.example.Lezedata.ui.dataPage.subVc.DataNodeListCell;
import com.example.Lezedata.ui.fragment.BaseFragment;
import com.example.Lezedata.util.GBTimeManager;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;

import bid.Bid;
import common.Common;

public class OrderPageFragment extends BaseFragment {
    private OrderPageViewModel mViewModel;
    public static OrderPageFragment newInstance() {
        return new OrderPageFragment();
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.tab_order_page_fragment, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mViewModel = new ViewModelProvider(this).get(OrderPageViewModel.class);
        // TODO: Use the ViewModel
    }

    private int pageStart = 0;
    private int pageSize = 20;
    private SmartRefreshLayout refreshLayout;
    private ListView listView;
    private ArrayList<Bid.NodeOrder> nodeBinds = new ArrayList<>();
    @Override
    public void onViewCreated(@NonNull @NotNull View view, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        listView = findViewById(R.id.listView);
//        time = findViewById(R.id.time);
        refreshLayout = findViewById(R.id.refreshLayout);
        refreshLayout.setEnableRefresh(false);
        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull @NotNull RefreshLayout refreshLayout) {
                loadData();
            }
        });
//        time.setText(String.format("更新日期"));
        listView.setAdapter(adapter);
        hudShow();
        loadData();
    }

    void loadData(){
        Common.PageInfo pageInfo = Common.PageInfo.newBuilder().setPageStart(pageStart).setPageSize(pageSize).build();
        Bid.GetUserOrdersRequest request = Bid.GetUserOrdersRequest.newBuilder().setUserId(GBUserManager.getUserId()).setPageInfo(pageInfo).build();
        GBNetRequest.stubUser().getUserOrders(request, ServerResponseObserver.observer(new ServerResponseObserver.requestListener<Bid.GetUserOrdersResponse>() {
            @Override
            public void complete(Bid.GetUserOrdersResponse value, GBError error) {
                refreshLayout.finishLoadMore();
                hudDismiss();
                if (error == null){
                    if (pageStart == 0){
                        nodeBinds.clear();
                    }
                    nodeBinds.addAll(value.getOrdersList());
                    if (value.getCount() >= pageSize){
                        pageStart++;
                    }
                    refreshLayout.setEnableLoadMore(value.getOrdersCount() >= pageSize);
                    adapter.notifyDataSetChanged();
                }
            }
        }));
    }

    BaseAdapter adapter = new BaseAdapter() {
        @Override
        public int getCount() {
            return nodeBinds.size();
        }

        @Override
        public Bid.NodeOrder getItem(int position) {
            return nodeBinds.get(position);
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null){
                convertView = OrderListCell.newInstance(getContext(),listView);
            }
            OrderListCell cell = (OrderListCell)convertView;
            cell.setPool(getItem(position));
            return convertView;
        }
    };
}